import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, OnInit, ViewChild, Input } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ProfileServiceProxy, TaskServiceProxy, WorkflowServiceProxy, OutsouringServiceProxy, OutsourcingWorkFlowServiceProxy, UserServiceProxy, ProcessingOrderDto, ProcessingViewDto, ProcessingOutPutDto, ExecutionPointerDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { IOrganizationUnitsTreeComponentData, OrganizationUnitsTreeComponent } from '../shared/organization-unit-tree.component';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { TaskService } from '../task/Task.service';
import { accountModuleAnimation } from '@shared/animations/routerTransition';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';

@Component({
  selector: 'ProcessingOrderAudit',
  templateUrl: './ProcessingOrderAudit.component.html',
  styleUrls: ['./ProcessingOrderAudit.component.css']
})
export class ProcessingOrderAuditComponent extends AppComponentBase implements OnInit {
  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('paginator') paginator: Paginator;
  @ViewChild('taskdataTable') taskdataTable: Table;
  @ViewChild('dataTable') dataTable: Table;

  @Input() workflowid: any;
  @Output() out = new EventEmitter();

  active = false;
  saving = false;
  _processingOrderDto: ProcessingOrderDto = new ProcessingOrderDto();
  _processingViewDto: ProcessingViewDto[];
  detailsprimengTableHelper: PrimengTableHelper;
  ngOnInit(): void {

    console.log(this.workflowid);
    this.initForm();
  }
  constructor(injector: Injector,
    private route: ActivatedRoute,
    private _OutsourcingWorkFlowServiceProxy: OutsourcingWorkFlowServiceProxy,
    private _OutsouringServiceProxy: OutsouringServiceProxy,
    private _taskServiceProxy: TaskServiceProxy,
    private _userServiceProxy: UserServiceProxy,
    private _workflowService: WorkflowServiceProxy) {
    super(injector);
    this.detailsprimengTableHelper = new PrimengTableHelper();

  }

  initForm(): void {
    this._OutsourcingWorkFlowServiceProxy.getProcessingByWorkFlowId(this.workflowid).subscribe(dto => {
      this._processingOrderDto = dto.processingOrderDto;
      this._processingViewDto = dto.processingViewDtos;
      console.log(dto);
      this.primengTableHelper.totalRecordsCount = this._processingViewDto.length;
      this.primengTableHelper.records = this._processingViewDto;
      this.primengTableHelper.hideLoadingIndicator();

      _.forEach(this._processingViewDto, viewItem => {
        console.log(viewItem.taskForModelId);
        this._taskServiceProxy.getTaskForEdit(viewItem.taskForModelId)
          .subscribe(userResult => {
            console.log(userResult);
            viewItem.taskCode = userResult.displayName;
            viewItem.taskName = userResult.code;
            viewItem.picNo = userResult.picNo;
          });

          this._OutsouringServiceProxy.getOutsouringApplyAmountForTaskId(viewItem.taskForModelId).subscribe(result => {
            viewItem.outsouringAmount = result;
          });

        this.active = true;
      });
    });
  }

  audityes(): void {
    this._OutsourcingWorkFlowServiceProxy.processingOrderAuditFlow_execute(this.workflowid, 'yes').subscribe(dto => {
      console.log(dto);
      this.out.emit(dto);
    });
  }

  auditno(): void {
    this._OutsourcingWorkFlowServiceProxy.processingOrderAuditFlow_execute(this.workflowid, 'no').subscribe(dto => {
      console.log(dto);
      this.out.emit(dto);
    });
  }
 
  getAuditName(item: ExecutionPointerDto) {
    return item.extensionAttributes.Prompt;
  }
}
